<?php
class AjaxRequestController extends CController
{
	public $layout='ajax';
	private $_model;

	/**
	 * Index action is the default action in a controller.
	 */
	public function actionIndex()
	{

	}
	public function actionEquipmentInRoom()
	{
		$roomId = addslashes($_GET['room']);
		$this->render('equipment_in_room', array('roomId'=>$roomId));
	}
	public function actionClassroomRequestBooking()
	{
		$requestType = addslashes($_GET['RequestType']);
		$this->render('classroom_request_booking', array('requestType'=>$requestType));
	}
	public function actionAvailableClassroomBookingPeriod()
	{
		$date = addslashes($_GET['Date']);
		$requestType = addslashes($_GET['RequestType']);
		$semester = addslashes($_GET['Semester']);
		$day = addslashes($_GET['Day']);
		$start = addslashes($_GET['Start']);
		$end = addslashes($_GET['End']);
		$this->render('available-classroom-booking-period', array('requestType'=>$requestType, 'date'=>$date,'start'=>$start,'end'=>$end,'semester'=>$semester,'day'=>$day,));
	}
	public function actionRequest()
	{
		// Authen Login
		if(!UserLoginUtil::isLogin()){
			$this->redirect(Yii::app()->createUrl('management/login'));
		}
		// Render
		$model = new RequestBooking();
		$this->render('main', array('data'=>$model));
	}

	public function actionRequestBorrowExecutiveApprove()
	{
		$location = addslashes($_GET['Location']);
		$this->render('request-borrow-executive-approve', array('location'=>$location,));
	}
	public function actionRequestServiceOther()
	{
		$checked = addslashes($_GET['checked']);
		$this->render('request-service-other', array('checked'=>$checked,));
	}
	public function actionRoomSchedule()
	{
		$roomId = addslashes($_GET['room_id']);
		$month = addslashes($_GET['month']);
		$year = addslashes($_GET['year']);
		$this->render('room-schedule', array('roomId'=>$roomId, 'month'=>$month,'year'=>$year,));
	}
	public function actionAvailableRoomSchedule()
	{
		if(isset($_SESSION['date_filter'])){
			$_GET['date_filter'] = $_SESSION['date_filter'];
			unset($_SESSION['date_filter']);
		}
		if(isset($_SESSION['equipment_type_id'])){
			$_GET['equipment_type_id'] = $_SESSION['equipment_type_id'];
			unset($_SESSION['equipment_type_id']);
		}
		if(isset($_SESSION['start_period'])){
			$_GET['start_period'] = $_SESSION['start_period'];
			unset($_SESSION['start_period']);
		}

		// Render
		if(isset($_GET['date_filter'])) {
			$date = $_GET['date_filter'];
		} else {
			$date = date('d-m-Y');
		}

		$equipmentTypeId = addslashes($_GET['equipment_type_id']);

		$equipmentType = $_GET['equipment_type'];
		if($_GET['start_period'] != '') {
			$timeStart = $_GET['start_period'];
		} else {
			$timeStart = 0;
			$timeEnd = 0;
		}
		if($_GET['room_id'] != '') {
			$roomId = $_GET['room_id'];
		} else {
			unset($roomId);
		}

		if(isset($_GET['end_period'])) {
			$timeEnd = $_GET['end_period'];
			if($timeStart == 0) {
				$timeEnd = 0;
			}
		} else {
			$timeEnd = $timeStart;
		}

		if($equipmentType != '') {
			$_SESSION['request_equipment'] = $equipmentType;
		} else {
			unset($_SESSION['request_equipment']);
		}
		if($date != '') {
			$_SESSION['request_date'] = $date;
		} else {
			unset($_SESSION['request_date']);
		}
		if($timeStart != '') {
			$_SESSION['request_time_start'] = $timeStart;
		} else {
			unset($_SESSION['request_time_start']);
		}
		if($timeEnd != '') {
			$_SESSION['request_time_end'] = $timeEnd;
		} else {
			unset($_SESSION['request_time_end']);
		}

		if(date != '') {
			$date = DateTimeUtil::convertDateFormat($date, 'yyyy-mm-dd');
		}
		$condition = "room_group_id = 1";
		$unavailableSpecifyRoomIds = RequestUtil::getUnavailableSpecifyRoom($date, $timeStart, $timeEnd, null, null);
		if(count($unavailableSpecifyRoomIds) > 0) {
			$exceptRoomId = '(';
			foreach($unavailableSpecifyRoomIds as $unavailableSpecifyRoomId) {
				$exceptRoomId = $exceptRoomId.$unavailableSpecifyRoomId.', ';
			}
			$exceptRoomId = substr($exceptRoomId, 0, strlen($exceptRoomId) - 2);
			$exceptRoomId = $exceptRoomId.')';
			$condition = $condition.' AND id not in '.$exceptRoomId;

		}
		if(isset($roomId)) {
			$condition = $condition." AND id = '".$roomId."'";
		}
		if(isset($equipmentTypeId) && $equipmentTypeId != '') {
			$rs = RequestUtil::getRoomHasEquipment($equipmentTypeId);
			$condition = $condition." AND id IN ".$rs;
		}
		// 		echo $condition;
		$model = Room::model()->findAll(array('condition'=>$condition));


		$this->render('available-room-schedule', array('data'=>$model,'date'=>$date));
	}
	public function actionAvailableRoomScheduleMeeting()
	{
		// Render
		if(isset($_GET['date_filter'])) {
			$date = $_GET['date_filter'];
		} else {
			$date = date('d-m-Y');
		}
		$equipmentTypeId = addslashes($_GET['equipment_type_id']);
		$equipmentType = $_GET['equipment_type'];
		if($_GET['start_period'] != '') {
			$timeStart = $_GET['start_period'];
		} else {
			$timeStart = 0;
			$timeEnd = 0;
		}
		if($_GET['room_id'] != '') {
			$roomId = $_GET['room_id'];
		} else {
			unset($roomId);
		}

		if(isset($_GET['end_period'])) {
			$timeEnd = $_GET['end_period'];
			if($timeStart == 0) {
				$timeEnd = 0;
			}
		} else {
			$timeEnd = $timeStart;
		}

		if($equipmentType != '') {
			$_SESSION['request_equipment'] = $equipmentType;
		} else {
			unset($_SESSION['request_equipment']);
		}
		if($date != '') {
			$_SESSION['request_date'] = $date;
		} else {
			unset($_SESSION['request_date']);
		}
		if($timeStart != '') {
			$_SESSION['request_time_start'] = $timeStart;
		} else {
			unset($_SESSION['request_time_start']);
		}
		if($timeEnd != '') {
			$_SESSION['request_time_end'] = $timeEnd;
		} else {
			unset($_SESSION['request_time_end']);
		}

		if(date != '') {
			$date = DateTimeUtil::convertDateFormat($date, 'yyyy-mm-dd');
		}
		$condition = "room_group_id = 2";
		$unavailableSpecifyRoomIds = RequestUtil::getUnavailableSpecifyRoom($date, $timeStart, $timeEnd, null, null);
		if(count($unavailableSpecifyRoomIds) > 0) {
			$exceptRoomId = '(';
			foreach($unavailableSpecifyRoomIds as $unavailableSpecifyRoomId) {
				$exceptRoomId = $exceptRoomId.$unavailableSpecifyRoomId.', ';
			}
			$exceptRoomId = substr($exceptRoomId, 0, strlen($exceptRoomId) - 2);
			$exceptRoomId = $exceptRoomId.')';
			$condition = $condition.' AND id not in '.$exceptRoomId;

		}
		if(isset($roomId)) {
			$condition = $condition." AND id = '".$roomId."'";
		}
		if(isset($equipmentTypeId) && $equipmentTypeId != '') {
			$rs = RequestUtil::getRoomHasEquipment($equipmentTypeId);
			$condition = $condition." AND id IN ".$rs;
		}
		// 		echo $condition;
		$model = Room::model()->findAll(array('condition'=>$condition));
		$this->render('available-room-schedule-meeting', array('data'=>$model,'date'=>$date));
	}

	public function actionSetBookingAttr() {
		$roomId = addslashes($_GET['room_id']);
		$periodStart = addslashes($_GET['period_start']);
		$periodEnd = addslashes($_GET['period_end']);
		$requestDate = addslashes($_GET['request_date']);
		$type = addslashes($_GET['type']);
		$this->render('booking-attr', array('type'=>$type, 'roomId'=>$roomId, 'periodStart'=>$periodStart, 'periodEnd'=>$periodEnd, 'requestDate'=>$requestDate));
	}
	public function actionRequestServiceTypeDetail() {
		$id = addslashes($_GET['id']);
		$datas = RequestServiceTypeDetail::model()->findAll(array('condition'=>"request_service_type_id='".$id."'"));
		$this->render('request_service_type_detail', array('datas'=>$datas));
	}

	public function actionUpload() {

		// A list of permitted file extensions
		$allowed = array('png', 'jpg', 'gif','zip');
		if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){
			$extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);
			if(!in_array(strtolower($extension), $allowed)){
				echo '{"status":"error"}';
				exit;
			}
			if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/'.$_FILES['upl']['name'])){
				echo '{"status":"success"}';
				exit;
			}
		}
		echo '{"status":"error"}';
		exit;
	}


}